;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;kernel/bootup.asm
;Boot code for AWOS
;
;Copyright (c) 2006 Andrew Wilcox. All rights reserved.
;
;Redistribution and use in source and binary forms, with or
;without modification, are permitted provided that the following
;conditions are met:
;
;1. Redistributions of source code must retain the above copyright
;notice, this list of conditions and the following disclaimer.
;
;2. Redistributions in binary form must reproduce the above
;copyright notice, this list of conditions and the following
;disclaimer in the documentation and/or other materials provided
;with the distribution.
;
;3. The end-user documentation included with the redistribution,
;if any, must include the following acknowledgment:
;
;"This product includes software developed by Andrew Wilcox for
; AWOS. (http://code.google.com/p/awos)."
;
;Alternately, this acknowledgment may appear in the software
;itself, if and wherever such third-party acknowledgments normally
;appear.
;
;4. The names "AWOS" and "Andrew Wilcox" must not be used to
;endorse or promote products derived from this software without
;prior written permission. For written permission, please contact
;Andrew.R.Wilcox@Gmail.com.
;
;5. Products derived from this software may not be called "AWOS"
; nor may "AWOS" appear in their name, without prior written
; permission of Andrew Wilcox.
;
;THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
;WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
;OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
;DISCLAIMED. IN NO EVENT SHALL ANDREW WILCOX BE LIABLE FOR ANY
;DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
;CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
;OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
;OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
;LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
;(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
;USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
;DAMAGE.
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		;NASM-IDE ASM Assistant Assembler Project File
BITS 32		;Set code generation to 32 bit mode
	jmp start
	jmp start
start:
	mov esp,sys_stack
	jmp overgrub
	jmp overgrub
	jmp overgrub
	jmp overgrub

ALIGN 4
mboot:
	MULTIBOOT_PAGE_ALIGN	equ 1<<0
	MULTIBOOT_MEMORY_INFO	equ 1<<1
	MULTIBOOT_AOUT_KLUDGE	equ 1<<16
	MULTIBOOT_HEADER_MAGIC	equ 0x1BADB002
	MULTIBOOT_HEADER_FLAGS	equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO | MULTIBOOT_AOUT_KLUDGE
	MULTIBOOT_CHECKSUM		equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
	EXTERN code, bss, end

	dd MULTIBOOT_HEADER_MAGIC
	dd MULTIBOOT_HEADER_FLAGS
	dd MULTIBOOT_CHECKSUM

	dd mboot
	dd code
	dd bss
	dd end
	dd start

overgrub:
	global start
	mov ebp,0
	extern k_main
	push ebx
	call k_main
	jmp $

global gdt_flush
extern gp
gdt_flush:
    lgdt [gp]
    mov ax,0x10
    mov ds,ax
    mov es,ax
    mov fs,ax
    mov gs,ax
    jmp 0x08:flush2
flush2:
    ret

global idt_load
extern idtp
idt_load:
    lidt [idtp]
    ret

;;;;ISRs;;;;
global isr00
global isr01
global isr02
global isr03
global isr04
global isr05
global isr06
global isr07
global isr08
global isr09
global isr0A
global isr0B
global isr0C
global isr0D
global isr0E
global isr0F
global isr10
global isr11
global isr12
global isr13
global isr14
global isr15
global isr16
global isr17
global isr18
global isr19
global isr1A
global isr1B
global isr1C
global isr1D
global isr1E
global isr1F
global isr20
global isr21
global isr22
global isr23
global isr24
global isr25
global isr26
global isr27
global isr28
global isr29
global isr2A
global isr2B
global isr2C
global isr2D
global isr2E
global isr2F
global isr30
global isr31
global isr32
global isr33
global isr34
global isr35
global isr36
global isr37
global isr38
global isr39
global isr3A
global isr3B
global isr3C
global isr3D
global isr3E
global isr3F

isr00:
    cli
    push byte 0
    push byte 0
    jmp isr_common

isr01:
    cli
    push byte 0
    push byte 1
    jmp isr_common

isr02:
    cli
    push byte 0
    push byte 2
    jmp isr_common

isr03:
    cli
    push byte 0
    push byte 3
    jmp isr_common

isr04:
    cli
    push byte 0
    push byte 4
    jmp isr_common

isr05:
    cli
    push byte 0
    push byte 5
    jmp isr_common

isr06:
    cli
    push byte 0
    push byte 6
    jmp isr_common

isr07:
    cli
    push byte 0
    push byte 7
    jmp isr_common

isr08:
    cli
    push byte 8
    jmp isr_common

isr09:
    cli
    push byte 0
    push byte 9
    jmp isr_common

isr0A:
    cli
    push byte 10
    jmp isr_common

isr0B:
    cli
    push byte 11
    jmp isr_common

isr0C:
    cli
    push byte 12
    jmp isr_common

isr0D:
    cli
    push byte 13
    jmp isr_common

isr0E:
    cli
    push byte 14
    jmp isr_common

isr0F:
    cli
    push byte 0
    push byte 15
    jmp isr_common

isr10:
    cli
    push byte 0
    push byte 16
    jmp isr_common

isr11:
    cli
    push byte 0
    push byte 17
    jmp isr_common

isr12:
    cli
    push byte 0
    push byte 18
    jmp isr_common

isr13:
    cli
    push byte 0
    push byte 19
    jmp isr_common

isr14:
    cli
    push byte 0
    push byte 20
    jmp isr_common

isr15:
    cli
    push byte 0
    push byte 21
    jmp isr_common

isr16:
    cli
    push byte 0
    push byte 22
    jmp isr_common

isr17:
    cli
    push byte 0
    push byte 23
    jmp isr_common

isr18:
    cli
    push byte 0
    push byte 24
    jmp isr_common

isr19:
    cli
    push byte 0
    push byte 25
    jmp isr_common

isr1A:
    cli
    push byte 0
    push byte 26
    jmp isr_common

isr1B:
    cli
    push byte 0
    push byte 27
    jmp isr_common

isr1C:
    cli
    push byte 0
    push byte 28
    jmp isr_common

isr1D:
    cli
    push byte 0
    push byte 29
    jmp isr_common

isr1E:
    cli
    push byte 0
    push byte 30
    jmp isr_common

isr1F:
    cli
    push byte 0
    push byte 31
    jmp isr_common
    
isr20:
	cli
	push byte 0
	push byte 32
	jmp isr_common
	
isr21:
	cli
	push byte 0
	push byte 33
	jmp isr_common
	
isr22:
	cli
	push byte 0
	push byte 34
	jmp isr_common
	
isr23:
	cli
	push byte 0
	push byte 35
	jmp isr_common
	
isr24:
	cli
	push byte 0
	push byte 36
	jmp isr_common

isr25:
	cli
	push byte 0
	push byte 37
	jmp isr_common

isr26:
	cli
	push byte 0
	push byte 38
	jmp isr_common
	
isr27:
	cli
	push byte 0
	push byte 39
	jmp isr_common
	
isr28:
	cli
	push byte 0
	push byte 40
	jmp isr_common

isr29:
	cli
	push byte 0
	push byte 41
	jmp isr_common

isr2A:
	cli
	push byte 0
	push byte 42
	jmp isr_common
	
isr2B:
	cli
	push byte 0
	push byte 43
	jmp isr_common
	
isr2C:
	cli
	push byte 0
	push byte 44
	jmp isr_common
	
isr2D:
	cli
	push byte 0
	push byte 45
	jmp isr_common
	
isr2E:
	cli
	push byte 0
	push byte 46
	jmp isr_common
	
isr2F:
	cli
	push byte 0
	push byte 47
	jmp isr_common

isr30:
	cli
	push byte 0
	push byte 48
	jmp isr_common
	
isr31:
	cli
	push byte 0
	push byte 49
	jmp isr_common
	
isr32:
	cli
	push byte 0
	push byte 50
	jmp isr_common
	
isr33:
	cli
	push byte 0
	push byte 51
	jmp isr_common

isr34:
	cli
	push byte 0
	push byte 52
	jmp isr_common
	
isr35:
	cli
	push byte 0
	push byte 53
	jmp isr_common
	
isr36:
	cli
	push byte 0
	push byte 54
	jmp isr_common
	
isr37:
	cli
	push byte 0
	push byte 55
	jmp isr_common

isr38:
	cli
	push byte 0
	push byte 56
	jmp isr_common
	
isr39:
	cli
	push byte 0
	push byte 57
	jmp isr_common
	
isr3A:
	cli
	push byte 0
	push byte 58
	jmp isr_common
	
isr3B:
	cli
	push byte 0
	push byte 59
	jmp isr_common

isr3C:
	cli
	push byte 0
	push byte 60
	jmp isr_common
	
isr3D:
	cli
	push byte 0
	push byte 61
	jmp isr_common
	
isr3E:
	cli
	push byte 0
	push byte 62
	jmp isr_common
	
isr3F:
	cli
	push byte 0
	push byte 63
	jmp isr_common

extern fault_handler

isr_common:
    pusha
    push ds
    push es
    push fs
    push gs
    mov ax,0x10
    mov ds,ax
    mov es,ax
    mov fs,ax
    mov gs,ax
    mov eax,esp
    push eax
    mov eax,fault_handler
    call eax
    pop eax
    pop gs
    pop fs
    pop es
    pop ds
    popa
    add esp,8
    iret

global irq00
global irq01
global irq02
global irq03
global irq04
global irq05
global irq06
global irq07
global irq08
global irq09
global irq0A
global irq0B
global irq0C
global irq0D
global irq0E
global irq0F

irq00:
    cli
    push byte 0
    push byte 0
    jmp irq_common

irq01:
    cli
    push byte 0
    push byte 1
    jmp irq_common

irq02:
    cli
    push byte 0
    push byte 2
    jmp irq_common

irq03:
    cli
    push byte 0
    push byte 3
    jmp irq_common

irq04:
    cli
    push byte 0
    push byte 4
    jmp irq_common

irq05:
    cli
    push byte 0
    push byte 5
    jmp irq_common

irq06:
    cli
    push byte 0
    push byte 6
    jmp irq_common

irq07:
    cli
    push byte 0
    push byte 7
    jmp irq_common

irq08:
    cli
    push byte 0
    push byte 8
    jmp irq_common

irq09:
    cli
    push byte 0
    push byte 9
    jmp irq_common

irq0A:
    cli
    push byte 0
    push byte 0Ah
    jmp irq_common

irq0B:
    cli
    push byte 0
    push byte 0Bh
    jmp irq_common

irq0C:
    cli
    push byte 0
    push byte 0Ch
    jmp irq_common

irq0D:
    cli
    push byte 0
    push byte 0Dh
    jmp irq_common

irq0E:
    cli
    push byte 0
    push byte 0Eh
    jmp irq_common

irq0F:
    cli
    push byte 0
    push byte 0Fh
    jmp irq_common

extern KiIrqHandler

irq_common:
    pusha
    push ds
    push es
    push fs
    push gs
    mov ax,0x10
    mov ds,ax
    mov es,ax
    mov fs,ax
    mov gs,ax
    mov eax,esp
    push eax
    mov eax,KiIrqHandler
    call eax
    pop eax
    pop gs
    pop fs
    pop es
    pop ds
    popa
    add esp,8
    iret

SECTION .bss
    resb 8192
sys_stack: